package com.tarena.lbs.marketing.repository.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.marketing.mapper.UserReceiveCouponMapper;
import com.tarena.lbs.marketing.repository.UserReceiveCouponRepo;
import com.tarena.lbs.pojo.marketing.bo.UserReceiveCouponBO;
import com.tarena.lbs.pojo.marketing.dos.UserReceiveCouponDO;
import com.tarena.lbs.pojo.marketing.query.UserCouponQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class UserReceiveCouponRepoImpl implements UserReceiveCouponRepo {
    @Autowired
    private UserReceiveCouponMapper userReceiveCouponMapper;

    @Override
    public void saveUserReceiveCoupon(UserReceiveCouponBO userReceiveCouponBO) {
        //直接insert新增
        UserReceiveCouponDO aDo=new UserReceiveCouponDO();
        BeanUtils.copyProperties(userReceiveCouponBO,aDo);
        userReceiveCouponMapper.insert(aDo);
    }

    @Override
    public List<UserReceiveCouponBO> receiveCouponList(Integer userId, Integer couponId) {
        //SELECT * FROM user_receive_coupon where user_id=#{userId} and coupon_id=#{couponId}
        List<UserReceiveCouponDO> dos=userReceiveCouponMapper.selectReceiveRecordsByUserIdAndCouponId(userId,couponId);
        List<UserReceiveCouponBO> bos=null;
        if (dos!=null){
            bos=dos.stream().map(aDO->{
                UserReceiveCouponBO bo=new UserReceiveCouponBO();
                BeanUtils.copyProperties(aDO,bo);
                return bo;
            }).collect(Collectors.toList());
        }
        return bos;
    }

    @Override
    public BasePageList<UserReceiveCouponBO> userReceivePage(UserCouponQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        //select * from user_receive_coupon where user_id=#{userId}
        List<UserReceiveCouponDO> dos=userReceiveCouponMapper.selectReceiveRecordsByUserId(query);
        PageInfo<UserReceiveCouponDO> pageInfo=new PageInfo<>(dos);
        BasePageList basePageList=null;
        if(pageInfo.getTotal()>0){
            List<UserReceiveCouponBO> boPage=pageInfo.getList().stream().map(aDO->{
                UserReceiveCouponBO bo=new UserReceiveCouponBO();
                BeanUtils.copyProperties(aDO,bo);
                return bo;
            }).collect(Collectors.toList());
            basePageList=new BasePageList(boPage,pageInfo.getTotal());
        }else{
            basePageList=new BasePageList(null,0l);
        }
        return basePageList;
    }

    @Override
    public UserReceiveCouponBO getUserReceiveDetail(String couponCode) {
        UserReceiveCouponDO aDo=userReceiveCouponMapper.selectReceiveRecordsByCode(couponCode);
        if (aDo!=null){
            UserReceiveCouponBO bo=new UserReceiveCouponBO();
            BeanUtils.copyProperties(aDo,bo);
            return bo;
        }
        return null;
    }
}
